<script setup lang="ts" name="patient">
import { getPatientList, addPatient, updatePatient, delPatient } from '@/services/user'
import type { Patient } from '@/types/user'
import { showToast, showSuccessToast, showConfirmDialog } from 'vant'
import { onMounted, ref, computed } from 'vue'
import IDValidator from 'id-validator'
import { useRoute, useRouter } from 'vue-router'
import { useConsultStore } from '@/stores'
// 1. 获取用户信息
const patientList = ref<Patient[]>([])
// 封装获取用户信息的方法
const getList = async () => {
  const res = await getPatientList()
  patientList.value = res.data
}
onMounted(async () => getList())
// 2. 显示侧边栏
const show = ref(false)
// 注册侧边栏显示事件
const shoPop = () => {
  // 清空数据
  patient.value = {
    name: '',
    idCard: '',
    gender: 1,
    defaultFlag: 0
  }
  show.value = true
}
// 3. 绑定侧边栏的表单结构
const options = [
  { label: '男', value: 1 },
  { label: '女', value: 0 }
]
// 定义数据源
const patient = ref<Patient>({
  name: '',
  idCard: '',
  gender: 1,
  defaultFlag: 0
})
var Validator = new IDValidator()
// 4. 数据校验
const submit = async () => {
  // 名字非空
  if (patient.value.name.trim() === '') return showToast('名字不能为空')
  // 身份证非空
  if (patient.value.idCard.trim() === '') return showToast('身份证不能为空')
  // 校验身份证信息是否合法
  if (!Validator.isValid(patient.value.idCard)) return showToast('身份证不合法')
  // 校验身份证号码与性别是否一致
  const { sex } = Validator.getInfo(patient.value.idCard)
  if (sex !== patient.value.gender) return showToast('性别与身份证不符')
  // 判断用户信息是否有ID
  patient.value.id
    // 有则调用编辑接口
    ? await updatePatient(patient.value)
    // 无则调用添加接口
    : await addPatient(patient.value)
  // 提示新增成功
  showSuccessToast(patient.value.id ? '编辑成功' : '新增成功')
  // 关闭面板
  show.value = false
  // 更新数据
  getList()
}
// 编辑
const opEdit = (i: Patient) => {
  // 显示面板
  show.value = true
  // 将数据赋值
  patient.value = {
    name: i.name,
    idCard: i.idCard,
    defaultFlag: i.defaultFlag,
    id: i.id,
    gender: i.gender
  }
}
// 删除
const remove = async() => {
  try {
    // 弹出确认框
    await showConfirmDialog({
      title: '温馨提示',
      message: '是否删除当前患者'
    })
    // 调用删除接口，对id非空断言
    const res = await delPatient(patient.value.id!)
    // 调用成功提示
    showSuccessToast(res.message)
    // 关闭面板
    show.value = false
    // 调用获取用户信息
    getList()
  }
  catch {} 
}
// 接受区别的表示
const route = useRoute()
const isChange = computed<boolean>(() => {
  const params = route.query.isChange
  if(params) return true
  return false
})

// 选中效果
const patientId = ref<string>()
// 设置选中的 id
const selectId = (id: string | undefined) => {
  if (!isChange.value) return
  patientId.value = id
}
onMounted(async () => {
  await getList()
  // 默认选中
  // 判断是否存在默认患者
  const obj = patientList.value.find((item) => item.defaultFlag)
  if (patientList.value.length === 0) return
  if (!isChange.value) return
  if (obj) {
    // 将默认患者的 id 保存起来
    patientId.value = obj.id
  } else {
    // 将第一个患者信息当作默认值
    patientId.value = patientList.value[0].id
  }
})
// 下一步
const store = useConsultStore()
const router = useRouter()
const next = () => {
  if(!patientId.value) return
  store.setPatientId(patientId.value)
  router.push('/consult/pay')
}
</script>

<template>
  <div class="patient-page">
    <cp-nav-bar :title="isChange ? '选择患者' : '家庭档案'" />
    <!-- 头部提示 -->
    <div class="patient-change" v-if="isChange">
      <h3>请选择患者信息</h3>
      <p>以便医生给出更准确的治疗，信息仅医生可见</p>
    </div>
    <div class="list">
      <div class="item" v-for="(i, index) in patientList" :key="index" :class="{ selected: patientId === i.id }" @click="selectId(i.id)">
        <div class="info">
          <span class="name">{{ i.name }}</span>
          <span class="id">{{ i.idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1********$2') }}</span>
          <span>{{ i.genderValue }}</span>
          <span>{{ i.age }} 岁</span>
        </div>
        <div class="icon" @click="opEdit(i)">
          <cp-icon name="user-edit" />
        </div>
        <div v-if="i.defaultFlag" class="tag">默认</div>
      </div>
      <div v-if="patientList.length < 6" class="add" @click="shoPop">
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
      <div class="tip">最多可添加 6 人</div>
      <!-- 底部按钮 -->
      <div class="patient-next" v-if="isChange">
        <van-button type="primary" round block @click="next">下一步</van-button>
      </div>
    </div>
    <!-- 侧边栏 -->
    <van-popup :show="show" @update:show="show = $event" position="right">
      <!-- 顶部导航 -->
      <cp-nav-bar :title="patient.id ? '编辑患者' : '添加患者'" right-text="保存" :back="() => show = false"
        @click-right="submit" />
      <van-form autocomplete="off">
        <van-field v-model="patient.name" label="真实姓名" placeholder="请输入真实姓名" />
        <van-field v-model="patient.idCard" label="身份证号" placeholder="请输入身份证号" />
        <van-field label="性别">
          <!-- 单选按钮组件 -->
          <template #input>
            <cp-radio-btn v-model="patient.gender" :options="options" />
          </template>
        </van-field>
        <van-field label="默认就诊人">
          <template #input>
            <!-- <van-checkbox v-model="patient.defaultFlag" round /> -->
            <van-checkbox :model-value="patient.defaultFlag" @update:model-value="patient.defaultFlag = $event ? 1 : 0"
              round />
          </template>
        </van-field>
      </van-form>
      <!-- 删除 -->
      <van-action-bar v-if="patient.id">
        <van-action-bar-button @click="remove">删除</van-action-bar-button>
      </van-action-bar>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 46px 0 80px;
  .patient-change {
    padding: 15px;
    > h3 {
      font-weight: normal;
      margin-bottom: 5px;
    }
    > p {
      color: var(--cp-text2);
    }
  }
  .patient-next {
    padding: 15px;
    background-color: #fff;
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 80px;
    box-sizing: border-box;
  }
  :deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      .van-action-bar {
        padding: 0 10px;
        margin-bottom: 10px;
        .van-button {
          color: var(--cp-price);
          background-color: var(--cp-bg);
        }
      }
    }
  }

  .list {
    padding: 15px;

    .item {
      display: flex;
      align-items: center;
      padding: 15px;
      background-color: var(--cp-bg);
      border-radius: 8px;
      margin-bottom: 15px;
      position: relative;
      border: 1px solid var(--cp-bg);
      transition: all 0.3s;
      overflow: hidden;

      .info {
        display: flex;
        flex-wrap: wrap;
        flex: 1;

        span {
          color: var(--cp-tip);
          margin-right: 20px;
          line-height: 30px;

          &.name {
            font-size: 16px;
            color: var(--cp-text1);
            width: 80px;
            margin-right: 0;
          }

          &.id {
            color: var(--cp-text2);
            width: 170px;
          }
        }
      }

      .icon {
        color: var(--cp-tag);
        width: 20px;
        text-align: center;
      }

      .tag {
        position: absolute;
        right: 60px;
        top: 21px;
        width: 30px;
        height: 16px;
        font-size: 10px;
        color: #fff;
        background-color: var(--cp-primary);
        border-radius: 2px;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      &.selected {
        border-color: var(--cp-primary);
        background-color: var(--cp-plain);

        .icon {
          color: var(--cp-primary);
        }
      }
    }
  }

  .add {
    background-color: var(--cp-bg);
    color: var(--cp-primary);
    text-align: center;
    padding: 15px 0;
    border-radius: 8px;

    .cp-icon {
      font-size: 24px;
    }
  }

  .tip {
    color: var(--cp-tag);
    padding: 12px 0;
  }
}
</style>
